package com.woniuxy.yuekeuser.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.controller.Form.CampsiteForm;
import com.woniuxy.yuekeuser.controller.Info.CampsiteInfo;
import com.woniuxy.yuekeuser.model.Campsite;
import com.woniuxy.yuekeuser.model.City;
import com.woniuxy.yuekeuser.model.HotelCar;
import com.woniuxy.yuekeuser.service.ICampsiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.service.ICampsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class CampsiteController {
    @Autowired
    private ICampsiteService iCampsiteService;

    //首页只展示8个营地
    @GetMapping("getIndexCampsite")
    public Result getIndexCampsite(){
        log.info("执行获取首页营地的方法");
        List<CampsiteForm> allCampsiteForm = iCampsiteService.getAllCampsiteByHeat();
        if (allCampsiteForm.size()<9){
            System.out.println(allCampsiteForm);
            return Result.success(allCampsiteForm);
        }else{
            //只返回热度最高的四个营地
            List<CampsiteForm> someCampsiteForm=allCampsiteForm.subList(0,7);
            System.out.println(someCampsiteForm);
            return Result.success(someCampsiteForm);
        }
    }

    @GetMapping("getCampsiteList")//分页查询所有营地
    public Result getCitys(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "")String findCampsite,@RequestParam(defaultValue = "3")int pageSize, @RequestParam(defaultValue = "")String cityName,@RequestParam(defaultValue = "0")int heat,@RequestParam(defaultValue = "0")int rating){
        log.info("分页查询全部营地的方法参数为{},{},{},{},{},{}",currPage,pageSize,findCampsite,cityName,heat,rating);
        if (cityName.equals("")){
            cityName=null;
        }
        Page<CampsiteForm> page= new Page<>();
        page.setCurrent(currPage);
        page.setSize(pageSize);
        IPage<CampsiteForm> campsiteList=iCampsiteService.findAllCampsiteList(page,cityName,findCampsite,heat,rating);
        for (int i = 0; i <campsiteList.getRecords().size() ; i++) {
            System.out.println(campsiteList.getRecords().get(i));
        }
        return Result.success(campsiteList);
    }
    @PostMapping("addCampsiteHeat")//添加营地的热度
    public Result addCampsiteHeat(int campsiteId,HttpSession session){
        log.info("执行点击营地页面增加营地热度的方法,参数为{}",campsiteId);
//        把营地id存入session
        session.setAttribute("campsiteId",campsiteId);
//        根据id查询当前营地
        Campsite campsite = iCampsiteService.findCampsiteById(campsiteId);
        campsite.setCampsiteHeat(campsite.getCampsiteHeat()+1);//热度加1
        iCampsiteService.addCampsiteHeat(campsite);

        return Result.success();
    }

    @GetMapping("getNowCampsite")
    public Result getNowCampsite(HttpSession session){
        log.info("获取营地页面详情的方法");
        if (session.getAttribute("campsiteId")!=null){
            int campsiteId = (int)session.getAttribute("campsiteId");
            CampsiteInfo campsiteInfo = iCampsiteService.nowCampsiteById(campsiteId);
            System.out.println(campsiteInfo);
            return Result.success(campsiteInfo);
        }
        return Result.fail("当前营地不存在");
    }


    @GetMapping("searchCampsiteByKeyWords")
    public Result searchCampsiteByKeyWords(String keyWord){
        List<Campsite> campsites = iCampsiteService.searchCampsiteByKeyWords(keyWord);



        return Result.success(campsites);
    }
}

